package com.suineg.pms.web.account;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Maps;
import com.suineg.pms.entity.Role;
import com.suineg.pms.entity.User;
import com.suineg.pms.service.account.AccountService;

/**
 * 管理员管理用户的Controller.
 * 
 * @author calvin
 */
@Controller
@RequestMapping(value = "/admin/user")
public class UserAdminController {

	@Autowired
	private AccountService accountService;
	
	private static Map<String, String> allStatus = Maps.newHashMap();

	static {
		allStatus.put("enabled", "有效");
		allStatus.put("disabled", "无效");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		List<User> users = accountService.getAllUserInitialized();
		model.addAttribute("users", users);
		return "account/adminUserList";
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		model.addAttribute("user", accountService.getUser(id));
		model.addAttribute("allStatus", allStatus);
		model.addAttribute("allRoles", accountService.getAllRole());
		model.addAttribute("action", "update");
		return "account/adminUserForm";
	}
	
	@RequestMapping(value = "create",method = RequestMethod.GET)
	public String addForm(Model model){
		model.addAttribute("allStatus", allStatus);
		model.addAttribute("allRoles", accountService.getAllRole());
		//新new一个  防止前台找roleList时 出错
		User usr = new User();
		model.addAttribute("user", usr);
		return "account/register";
	}
	
	@RequiresPermissions("user:edit")
	@RequestMapping(value = "addSave",method = RequestMethod.POST)
	public String addSave(@Valid User user,@RequestParam(value = "roleList") List<Long> checkRoleList,RedirectAttributes redirectAttributes){
		for(Long l : checkRoleList){
			Role role = new Role(l);
			user.getRoleList().add(role);
		}
		accountService.saveUser(user);
		redirectAttributes.addFlashAttribute("message", "创建用户" + user.getName() + "成功");
		return "redirect:/admin/user";
	}
	
	@RequiresPermissions("user:edit")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("preloadUser") User user,
			@RequestParam(value = "roleList") List<Long> checkedRoleList,
			RedirectAttributes redirectAttributes) {
		user.getRoleList().clear();
		for (Long roleId : checkedRoleList) {
			Role role = new Role(roleId);
			user.getRoleList().add(role);
		}
		accountService.saveUser(user);
		redirectAttributes.addFlashAttribute("message", "更新用户" + user.getLoginName() + "成功");
		return "redirect:/admin/user";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		User user = accountService.getUser(id);
		accountService.deleteUser(id);
		redirectAttributes.addFlashAttribute("message", "删除用户" + user.getLoginName() + "成功");
		return "redirect:/admin/user";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出User对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadUser")
	public User getUser(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return accountService.getUser(id);
		}
		return null;
	}
	
	/**
	 * 不自动绑定对象中的roleList属性，另行处理。
	 */
	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.setDisallowedFields("roleList");
	}
}
